package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;


    @Override
    public PageVO<Supplier> findSupplierList(Integer pageNum, Integer pageSize,
                                             SupplierVO supplierVO) {

        //已经拥有的
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Supplier.class);

        String address = supplierVO.getAddress();
        String contact = supplierVO.getContact();
        String name = supplierVO.getName();

        Example.Criteria criteria = o.createCriteria();
        if(null!=address&&!"".equals(address)){
            criteria.andLike("address","%"+address+"%");
        }
        if(null!=contact&&!"".equals(contact)){
            criteria.andLike("contact","%"+contact+"%");
        }
        if(null!=name&&!"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }

        List<Supplier> supplierList = supplierMapper.selectByExample(o);
        PageInfo<Supplier> info=new PageInfo<>(supplierList);

        return new PageVO<>(info.getTotal(),supplierList);
    }

    //添加物资来源
    @Override
    public void add(Supplier supplier) {
        supplierMapper.insert(supplier);
    }

    //修改物资来源之回显数据
    @Override
    public Supplier edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        return supplier;
    }

    //修改物资来源
    @Override
    public void update(Supplier supplier) {
        supplierMapper.updateByPrimaryKey(supplier);
    }

    //删除物资来源
    @Override
    public void delete(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Supplier> findAll() {
        List<Supplier> supplierList = supplierMapper.selectAll();
        return supplierList;
    }


}
